package hk.ust.cse.comp3111.studentSchedule;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class ImportByCourse extends Fragment implements OnClickListener{
	
	AutoCompleteTextView tv;
	CourseDatabaseHandler db;
	List<String> courseCode;
	ArrayList<String> listItems;
	ArrayAdapter<String> adapter;
	Button selectButton;
	
	
	TextView courseCodeInput, title,textView2,lecture,tutorial,labortatory;
	Spinner lectureSpinner, tutorialSpinner, labSpinner;

	
	
	
	public ImportByCourse(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.import_by_course, container, false);
         
        return rootView;
    }
	

  

	@Override
	public void onStart() {
		super.onStart();
		db=new CourseDatabaseHandler(this.getActivity());
		selectButton= (Button) this.getView().findViewById(R.id.selectButton);
		selectButton.setOnClickListener(this);
		
		tv = (AutoCompleteTextView) this.getView().findViewById(R.id.courseCodeInput);
		List<String> courseCode = db.getAllCourseCode();
		ArrayList<String> listItems=new ArrayList<String>();
		
		for(String s : courseCode){
        	listItems.add(s);
        }
		
		
		//ListView myListView = (ListView) this.getView().findViewById(R.id.list);
        
        adapter=new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_dropdown_item_1line,listItems);
        
        tv.setAdapter(adapter);
        
	
        courseCodeInput = (TextView) this.getView().findViewById(R.id.courseCode);
		title = (TextView) this.getView().findViewById(R.id.title);
		textView2= (TextView) this.getView().findViewById(R.id.textView2);
		lecture= (TextView) this.getView().findViewById(R.id.lecture);
		tutorial= (TextView) this.getView().findViewById(R.id.tutorial);
		labortatory= (TextView) this.getView().findViewById(R.id.lab);
		
		lectureSpinner  =  (Spinner) this.getView().findViewById(R.id.lectureSpinner);
		tutorialSpinner =  (Spinner) this.getView().findViewById(R.id.tutorialSpinner);
		labSpinner		=  (Spinner) this.getView().findViewById(R.id.labSpinner);
		
		
		
		courseCodeInput.setVisibility(View.INVISIBLE);
		title.setVisibility(View.INVISIBLE);
		lectureSpinner.setVisibility(View.INVISIBLE);
		tutorialSpinner.setVisibility(View.INVISIBLE);
		labSpinner.setVisibility(View.INVISIBLE);
		textView2.setVisibility(View.INVISIBLE);
		lecture.setVisibility(View.INVISIBLE);
		tutorial.setVisibility(View.INVISIBLE);
		labortatory.setVisibility(View.INVISIBLE);
	
	}

	
	@Override
	public void onClick(View v) {
		
		
		String input = tv.getText().toString().toUpperCase();
		db=new CourseDatabaseHandler(this.getActivity());
		//List<Course> courseList = new ArrayList();
		//courseList = db.getCourseByCourseCode(input);
		
		//List<String> outout = new ArrayList<String>();
		//outout = db.test();
		
		
		
		lecture.setVisibility(View.VISIBLE);
		/*if(!courseList.isEmpty()) lecture.setText(Integer.toString(courseList.get(0).getClassNo()));
		else lecture.setText("Hello world");*/
		
		//lecture.setText(outout.get(0).toString());
		String out=new String();
		//out=courseCode.get(0);   // *****problem here
		lecture.setText("hello world");
		
	}
	
	

	
	

	/*Bundle bundle = new Bundle();
    bundle.putString("code", tv.getText().toString());
	Fragment fragment = new classSelection();
	fragment.setArguments(bundle);
	
	if (fragment != null) {
		
		FragmentTransaction transaction = getFragmentManager().beginTransaction();
		
		

		//transaction.remove(this);
		transaction.replace(R.id.container, fragment);
        transaction.commit();

		
    
		
	}*/
	
	/*// 1. create an intent pass class name or intent action name 
    Intent intent = new Intent("hk.ust.cse.comp3111.studentSchedule.classSelection");

    // 2. put key/value data
    intent.putExtra("message", tv.getText().toString());

    // 3. or you can add data to a bundle
    Bundle extras = new Bundle();
    extras.putString("status", "Data Received!");

    // 4. add bundle to intent
    intent.putExtras(extras);

    // 5. start the activity
    startActivity(intent);*/
	
}
